package jinyilw.swing.rlaf.ui;

import jinyilw.swing.WindowTools;
import jinyilw.swing.rlaf.theme.RRootPaneSet;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JToolBar;
import javax.swing.RootPaneContainer;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.RootPaneUI;
import javax.swing.plaf.basic.BasicToolBarUI;
import java.awt.Graphics;

/**
 * @author Jinyi
 */
public class RToolBarUI extends BasicToolBarUI
{
	private static final RRootPaneSet rootPaneSet = RRootPaneSet.getRootPaneSet();
	private static final float alpha = 0.8f;
	private static final int brightness = 120;

	public static ComponentUI createUI(JComponent c)
	{
		return new RToolBarUI();
	}

	@Override
	public void installUI(JComponent c)
	{
		super.installUI(c);
		c.setOpaque(false);
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
	}

	@Override
	protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
	{
		RootPaneContainer c = super.createFloatingWindow(toolbar);
		if (c instanceof JDialog)
		{
			RootPaneUI ui = c.getRootPane().getUI();
			if (ui instanceof RRootPaneUI)
				((RRootPaneUI) ui).setBrightness(brightness);
		}
		return c;
	}

	@Override
	protected DragWindow createDragWindow(JToolBar toolbar)
	{
		DragWindow window = super.createDragWindow(toolbar);
		WindowTools.setWindowOpacity(window, alpha);
		return window;
	}

	@Override
	protected void paintDragWindow(Graphics g)
	{
		dragWindow.setBackground(rootPaneSet.bgColor);
	}
}
